<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_ci_type_attr.class.php');


class TCMS_CI_Type_Attr_DBC extends TCMS_CI_Type_Attr implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select ci_type, ".
						"attr_type, ".
						"name, ".
						"required, ".
						"hid, ".
						"params, ".
						"multiple ".
					"from tcms_ci_type_attrs where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_ci_type($rez_obj->CI_TYPE);
			$this->set_attr_type($rez_obj->ATTR_TYPE);
			$this->set_name(stripslashes($rez_obj->NAME));
			$this->set_required($rez_obj->REQUIRED);
			$this->set_hid(stripslashes($rez_obj->HID));
			$this->set_params(stripslashes($rez_obj->PARAMS));
			$this->set_multiple($rez_obj->MULTIPLE);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_ci_type_attrs_seq');
		$ci_type = $this->get_ci_type();
		$attr_type = $this->get_attr_type();
		$name = addslashes($this->get_name());
		$required = $this->get_required() ? 1 : 0;
		$hid = addslashes($this->get_hid());
		$params = addslashes($this->get_params());
		$multiple = $this->get_multiple() ? 1 : 0;

		if ($id) {
			$sql = "insert into tcms_ci_type_attrs (id, ".
									      "ci_type, ".
									      "attr_type, ".
									      "name, ".
									      "required, ".
									      "hid, ".
									      "params, ".
									      "multiple ".
									      ") ".
								"values ".
									      "($id,".
									      "$ci_type, ".
									      "$attr_type, ".
									      "'$name', ".
									      "$required, ".
									      "'$hid', ".
									      "'$params', ".
									      "$multiple ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_ci_type_attrs_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$ci_type = $this->get_ci_type();
		$attr_type = $this->get_attr_type();
		$name = addslashes($this->get_name());
		$required = $this->get_required() ? 1 : 0;
		$hid = addslashes($this->get_hid());
		$params = addslashes($this->get_params());
		$multiple = $this->get_multiple() ? 1 : 0;

		$sql = "update tcms_ci_type_attrs set ".
						"ci_type = $ci_type, ".
						"attr_type = $attr_type, ".
						"name = '$name', ".
						"required = $required, ".
						"hid = '$hid', ".
						"params = '$params', ".
						"multiple = '$multiple' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function select_all_for_ci_type(DB_Connection $dbc, $ci_type_id) {
		tangra_if_not_int_throw_e($ci_type_id);

		$ret = array();
		$sql = "select cta.id as ctaid, ".
						"cta.name as ctaname, ".
						"cat.name as catname, ".
						"cta.required, ".
						"cta.params, ".
						"cta.multiple, ".
						"cta.hid as ctahid ".
					"from tcms_ci_type_attrs cta, ".
					 		"tcms_ci_attr_types cat ".
					 "where cta.ci_type = $ci_type_id and ".
					 		"cta.attr_type = cat.id";
		$rez = $dbc->execute($sql);
		while ($rez_obj = $rez->fetch_object()) {
			$hid = $rez_obj->CTAHID;
			$ret[$hid]['id'] = $rez_obj->CTAID;
			$ret[$hid]['name'] = $rez_obj->CTANAME;
			$ret[$hid]['type_name'] = $rez_obj->CATNAME;
			$ret[$hid]['required'] = $rez_obj->REQUIRED;
			$ret[$hid]['params'] = $rez_obj->PARAMS;
			$ret[$hid]['multiple'] = $rez_obj->MULTIPLE;
		}

		return $ret;
	}


	public function load_by_type_and_hid(DB_Connection $dbc, $ci_type_id, $hid) {
		tangra_if_not_int_throw_e($ci_type_id);
		$hid = addslashes($hid);
		$ret = false;

		$sql = "select id from tcms_ci_type_attrs where ci_type = $ci_type_id and hid = '$hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public static function is_unique_hid(DB_Connection $dbc, $ci_type_attr_id, $hid, $ci_type) {
		tangra_if_not_int_throw_e($ci_type_attr_id);
		$hid = addslashes($hid);
		tangra_if_not_int_throw_e($ci_type);

		$sql = "select id from tcms_ci_type_attrs where id <> $ci_type_attr_id and ci_type = $ci_type and hid = '$hid'";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_ci_type_attrs where id = $id";
		$dbc->execute($sql);
	}


	public static function select_distinct_attr_type_ids_for_ci_type(DB_Connection $dbc, $ci_type_id) {
		tangra_if_not_int_throw_e($ci_type_id);
		$sql = "select attr_type from tcms_ci_type_attrs where ci_type = $ci_type_id";
		$rez = $dbc->execute($sql);
		$ret = array();
		if ($rez_obj = $rez->fetch_object()) {
			if (!array_key_exists($rez_obj->ATTR_TYPE, $ret)) {
				$ret[$rez_obj->ATTR_TYPE] = $rez_obj->ATTR_TYPE;
			}
		}

		return $ret;
	}


	public static function delete_all_for_type(DB_Connection $dbc, $type_id) {
		tangra_if_not_int_throw_e($type_id);

		$sql = "delete from tcms_ci_type_attrs where ci_type = $type_id";
		$dbc->execute($sql);
	}
}